package com.snake.springboot.custom.service.assemble.points;


import com.snake.springboot.custom.api.model.bill.BillEnum;
import com.snake.springboot.custom.api.model.common.CommonVal;
import com.snake.springboot.custom.api.model.pay.PayActModel;
import com.snake.springboot.custom.api.model.pay.PayActRetModel;
import com.snake.springboot.custom.api.model.points.PointsChangeModel;
import com.snake.springboot.custom.api.model.points.PointsChangeType;
import com.snake.springboot.custom.api.model.points.PointsRgeRecordModel;
import com.snake.springboot.custom.api.model.points.PointsRgeTempModel;
import com.snake.springboot.custom.api.request.bill.BillPayedAddModel;
import com.snake.springboot.custom.api.request.bill.BillRetEdModel;
import com.snake.springboot.custom.api.request.points.PointsRechargeReq;
import com.snake.springboot.custom.api.request.points.PointsRetReq;
import com.snake.springboot.custom.api.request.points.PointsRgeRecordQuery;
import com.snake.springboot.custom.dao.mybatis.entity.points.PointsRgeRecordDO;
import org.snake.common.code.SerialNumberGeneratorFactory;
import org.snake.common.security.account.AccountData;
import org.snake.common.security.account.LoginAccount;
import org.snake.common.utils.PropertyUtils;

import java.math.BigDecimal;

/**
 * <p>算力管理-充值记录组装器
 * 类{@code PointsRgeRecordAssemble} </br>
 * 创建时间2025/2/20 17:57
 * @author yangxt
 * @see
 */
public class PointsRgeRecordAssemble {
    public static PointsRgeRecordDO recordDOAss(PointsRgeRecordModel model, boolean isAdd) {
        if (isAdd) {
            model.setRecordId(SerialNumberGeneratorFactory.buildId4Bean());
        }
        PointsRgeRecordDO recordDO = new PointsRgeRecordDO();
        PropertyUtils.copyProperties(recordDO,model);
        return recordDO;
    }

    /**
     * 算力充值记录组装器
     * @param model
     * @param tempModel
     * @return
     */
    public static PointsRgeRecordDO recordDOAss(PointsRechargeReq model, PointsRgeTempModel tempModel) {
        PointsRgeRecordDO recordDO = new PointsRgeRecordDO();
        PropertyUtils.copyProperties(recordDO,tempModel);
        recordDO.setRecordId(SerialNumberGeneratorFactory.buildId4Bean());
        if (recordDO.getPayAmount().compareTo(BigDecimal.ZERO) == 0) {
            recordDO.setBillId(SerialNumberGeneratorFactory.buildId4Bean());
            recordDO.setIsPay(CommonVal.Y);
        }
        recordDO.setCustomId(model.getCustomId());
        recordDO.setPayAccType(model.getPayAccType());
        AccountData data = LoginAccount.orNullUser();
        if (data != null) {
            recordDO.setOperator(data.getAccount());
            recordDO.setOperatorId(data.getAccountId());
        }
        recordDO.setByAccount(model.getByAccount());
        return recordDO;
    }
    public static PointsRgeRecordDO recordDOAss(PointsRgeRecordQuery model) {
        PointsRgeRecordDO recordDO = new PointsRgeRecordDO();
        PropertyUtils.copyProperties(recordDO,model);
        return recordDO;
    }

    public static PointsChangeModel changeModelAss(PointsRgeRecordDO recordDO,PointsChangeType type,String remark) {
        PointsChangeModel changeModel = new PointsChangeModel();
        changeModel.setChangePoints(recordDO.getPoints());
        changeModel.setChangeType(type.getChangeType());
        changeModel.setRemark(remark);
        changeModel.setParentId(recordDO.getRecordId());
        changeModel.setCustomId(recordDO.getCustomId());
        return changeModel;
    }

    public static PayActModel actModelAss(PointsRechargeReq model, PointsRgeRecordDO recordDO) {
        PayActModel actModel =new PayActModel();
        actModel.setPayAmount(recordDO.getPayAmount());
        actModel.setPaySource(model.getPaySource());
        actModel.setPayAccType(model.getPayAccType());
        actModel.setPayCode(recordDO.getRecordId());
        actModel.setModule("points-pay");
        actModel.setPayRemark("算力充值");
        return actModel;
    }

    public static PayActRetModel actRetModelAss(PointsRgeRecordDO recordDO, PointsRetReq model) {
        PayActRetModel actRetModel =new PayActRetModel();
        actRetModel.setRetRemark(model.getRetRemark());
        actRetModel.setPayId(recordDO.getPayId());
        actRetModel.setRetAmount(recordDO.getRetAmount());
        return actRetModel;
    }

    public static BillPayedAddModel toBillPayedModel(PointsRgeRecordDO recordDO) {

        BillPayedAddModel addModel = new BillPayedAddModel();
        PropertyUtils.copyProperties(addModel,recordDO);
        addModel.setBillType(BillEnum.BillType.POINTS_PAY.getCode());
        addModel.setGoodsName(recordDO.getTemplate());
        addModel.setPayState(CommonVal.Y);
        addModel.setPayTime(recordDO.getCreatedTime());
        addModel.setOrderState(BillEnum.OrderStatus.FINISHED.getCode());
        return addModel;
    }

    public static BillRetEdModel toBillRedEdModel(PointsRgeRecordDO recordDO) {
        BillRetEdModel billRetEdModel = new BillRetEdModel();
        billRetEdModel.setBillId(recordDO.getBillId());
        billRetEdModel.setRetAmount(recordDO.getRetAmount());
        billRetEdModel.setRetState(CommonVal.Y);
        return billRetEdModel;
    }
}
